from flask import jsonify,Blueprint,request
import json
from db import db1
from jwt1 import myjwt
from utils.acl import acl
from utils.red1 import red

bp_user = Blueprint('user',__name__)

def get_data():
    data = request.data
    if data:
        data = json.loads(data)
    else:
        data = request.form
    return data



@bp_user.route('/login',methods=['POST','GET'])
def login():
    data = get_data()
    print(data)
    sql = "select * from user where username='%s' and password='%s'" % (data['username'], data['password'])
    res = db1.find(sql)
    token = myjwt.jwt_encode({'data':{'uid':res['id']}})
    #vue页面
    pagelist = acl.get_page_reslist(res['id'])
    # print(pagelist)
    # 接口路由
    interfacelist = acl.get_interface_reslist(int(res['id']))
    print(interfacelist)

    key = 'uid' + str(res['id'])
    red.insert_set(key,interfacelist)
    if res:
        return jsonify(
            {'code': 200, 'userid': res['id'],'token':token,"url":pagelist})
    else:
        return jsonify({'code': 20010, 'mes': '用户名或密码错误'})


